package com.power.ding.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.file.model.FileVo;
import com.plf.core.common.file.FileController;
import com.power.ding.api.IDi_todoFeign;
import com.power.ding.exception.DingException;
import com.power.ding.model.Di_message;
import com.power.ding.model.Di_todo;
import com.power.ding.model.pageInfo.Di_todoPageInfo;
import com.power.ding.service.Di_messageService;
import com.power.ding.service.Di_todoService;
import com.power.ding.service.SystemService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author xiongzhiwen
 * @version 1.0
 * @description: 代办信息
 * @date 2024/3/29 18:06
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Di_deptController",tags={"代办信息"})
public class Di_todoController implements IDi_todoFeign {

    @Resource
    private Di_todoService di_todoService;
    @Resource
    private Di_messageService di_messageService;
    @Resource
    private SystemService systemService;
    @Resource
    private FileController fileController;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO sendToDo(Di_todo todo){
        Assert.notNull(todo, "todo不能为空");
        try {
            di_todoService.saveOrUpdate(todo);
            systemService.sendTodo(todo.getId());
            return ResponseDTO.succ();
        } catch (DingException e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return ResponseDTO.wrap(500, e.getMessage());
        }
    }

    @Override
    public ResponseDTO sendMessage(Di_message message) {
        Assert.notNull(message, "message不能为空");
        try {
            di_messageService.saveOrUpdate(message);
            systemService.sendMessage(message.getId());
            return ResponseDTO.succ();
        } catch (DingException e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return ResponseDTO.wrap(500, e.getMessage());
        }
    }


    @Override
    public ResponseDTO getTodoListByUserid(Di_todoPageInfo pageInfo) throws DingException {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        PageDataResponse data = di_todoService.getTodoListByUserid(pageInfo);
        return ResponseDTO.succData(data);
    }

    @Override
    public ResponseDTO getAttachByApplyid(String applyid, Integer mattype) {
        Assert.notNull(applyid, "applyid不能为空");
        Assert.notNull(mattype, "type不能为空");
        Map<String, Object> attach = di_todoService.getAttachByApplyid(applyid, mattype);
        return ResponseDTO.succData(attach);
    }

    @Override
    public ResponseDTO uploadFile(MultipartFile file) {
        try {
            ResponseDTO<FileVo> fileVoResponseDTO = fileController.minioUpload(file, null, null);
            if (ObjectUtil.isNotEmpty(fileVoResponseDTO) && fileVoResponseDTO.getCode() == ResponseDTO.succ().getCode()) {
                return ResponseDTO.succData(fileVoResponseDTO.getData());
            }
        } catch (Exception e) {
            return ResponseDTO.wrap(500, "文件保存异常");
        }
        return ResponseDTO.succ();

    }

    @Override
    public ResponseDTO getFileListByApplyId(String applyid, Integer mattype) throws Exception {
        List<Map<String, Object>> fileList = di_todoService.getFileListByApplyId(applyid, mattype);
        if (!CollectionUtils.isEmpty(fileList)) {
            for (Map<String, Object> item : fileList) {
                ResponseDTO file = fileController.get((String) item.get("attachid"));
                if (ObjectUtil.isNotEmpty(file) && file.getCode() == ResponseDTO.succ().getCode()) {
                    item.put("file", file.getData());
                }
            }
        }
        return ResponseDTO.succData(fileList);
    }

    @Override
    public ResponseDTO finishRespond(String id, List<MultipartFile> files) {
        try {
            this.di_todoService.finishRespond(id, files);
            return ResponseDTO.succ();
        } catch (Exception e) {
            log.error("完成待办失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, e.getMessage());
        }
    }
}
